
// TODO

// use yui console
// (done) make some classes and generalize
// make TAB key navigate trough layout

jQuery(function() {
  YUI().use('lang', 'dd', 'io', 'json', 'console', function(Y) {
    window.dcat_instance.setYUI(Y);
  });
});

Cat = {}

dcat = function() {
  this.init = [];
  this.Y = null;
}

dcat.prototype.oninit = function(callback) {
  if (this.Y != null) {
    callback.call(this, this.Y);
  } else {
    this.init.push(callback);
  }
}

dcat.prototype.setYUI = function(Y) {
  if (typeof(window.console) == 'undefined') {
    window.console = {
      log : function() {
        //Y.log(arguments, 'info');
      }
    }
  }

  this.Y = Y;
  for (var i in this.init) {
    if (Y.Lang.isFunction(this.init[i])) {
      this.init[i].call(this, Y);
    }
  }
}

dcat.layout = {}





// --- resizable ---------------------------------------------------------------

dcat.prototype.resizable = function(selector) {

}



// --- drag & drop -------------------------------------------------------------

dcat.prototype.draggable = function(selector) {
  var dd = new this.Y.DD.Drag({
    node: selector
  });  
  var parent = this.Y.get(selector).get('parentNode');
  if (parent.layout) {
    parent.layout.apply(this.Y.get(selector));
  } else {
    ////////console.log('drag: no layout for ', parent)
  }
  return dd;
}

dcat.prototype.droppable = function(selector, layout) {  
  var me = this.Y.get(selector);
  var drop = new this.Y.DD.Drop({
    node: selector
  });
  if (typeof(layout) == 'undefined') {
    layout = new dcat.layout.relative();
  }
  me.layout = layout;
  drop.on('drop:hit', function(e) {
    layout.drop.call(layout, this, e);
    e.stopPropagation();
  });
  var parent = me.get('parentNode');
  if (parent.layout) {
    parent.layout.apply(this.Y.get(selector));
  } else {
    ////////console.log('drop: no layout for ', parent)
  }
  return drop;
}


window.dcat_instance = new dcat();


